package com.baizhi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.dao.GuruDao;
import com.baizhi.entity.Guru;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.*;
import java.util.List;

/**
 * Created by 野马 on 2019/6/18.
 * 导出
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CmfzApplication.class)
public class EasyPoi {

    @Autowired
    private GuruDao guruDao;

    @Test
    public void daochu() throws IOException {

        List<Guru> guruList = guruDao.selectGuru(null, 0, 100);
        /**
         * 参数1 导出参数对象  ExportParams title 上师表的所有数据   sheetName gurus
         * 参数2  实体类的类对象
         * 参数3 被导出的所有数据
         *
         */

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("上师表的所有数据", "gu"), Guru.class, guruList);
        workbook.write(new FileOutputStream(new File("E://easyui.xls")));


    }

    @Test
    public void daoru() throws Exception {
        /**
         * ImportParams 导入参数对象
         * setTitleRows  设置标题的行数
         * setHeadRows   设置表头行数
         */

        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);

        FileInputStream fileInputStream = new FileInputStream(new File("E://easyui.xls"));
        List<Guru> guruList = ExcelImportUtil.importExcel(fileInputStream, Guru.class, params);

        for (Guru guru : guruList) {
            guruDao.insertGuru(guru);

        }

    }







}
